package display {
	import com.adobe.serialization.json.JSON;
	
	import flare.animate.Sequence;
	import flare.animate.Tween;
	import flare.vis.data.NodeSprite;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	
	import org.aswing.AsWingManager;
	import org.aswing.AssetPane;
	import org.aswing.BorderLayout;
	import org.aswing.BoxLayout;
	import org.aswing.Container;
	import org.aswing.JAccordion;
	import org.aswing.JFrame;
	import org.aswing.JLabel;
	import org.aswing.JList;
	import org.aswing.JLoadPane;
	import org.aswing.JPanel;
	import org.aswing.JScrollPane;
	import org.aswing.JViewport;
	import org.aswing.ListModel;
	import org.aswing.LoadIcon;
	import org.aswing.ScrollPaneLayout;
	import org.aswing.VectorListModel;
	import org.aswing.ext.GridList;
	
	import spark.primitives.Rect;
	
	import util.NodeListCellFactory;
	
	[SWF(width="800", height="600", backgroundColor="#ffffff", frameRate="30")]
	public class NodeSummarySprite extends Sprite {
		public var nameLabel:JLabel;
		public var picture:JLoadPane;
		public var frame:JFrame;
		public var accordion:JAccordion;
		public var activityTab:JScrollPane;
		public var friendTab:JScrollPane;
		public var activityList:JList;
		public var friendList:JList;
		
		public var hideTimer:Timer;
		
		protected var t_:Tween;
		
		public function NodeSummarySprite(stage:Stage) {
			super();

			AsWingManager.setRoot(stage);

			hideTimer = new Timer(1500);
			hideTimer.addEventListener(TimerEvent.TIMER, onHide);
			frame = new JFrame(null, "", false);
			frame.setClosable(true);
			frame.setDragable(false);
			frame.setResizable(false);
			frame.setFocusable(true);
			

			nameLabel = new JLabel(null);
			var pane:Container = frame.getContentPane(); 
//			pane.append(nameLabel);
			accordion = new JAccordion();
			var viewport:JViewport;
			
			activityTab = new JScrollPane();
			activityList = new JList(null, new NodeListCellFactory());
			activityTab.setView(activityList);

			
			
			friendTab = new JScrollPane();
			friendList = new JList(null, new NodeListCellFactory());
			friendTab.setView(friendList);

			accordion.appendTab(friendTab, 'Friends');
			accordion.appendTab(activityTab, 'Recent Activities');
			
			pane.append(accordion);
			frame.setSizeWH(200, 200);
//			frame.setX(1000);
//			frame.setY(20);
			// frame.show();
			
		}
		
		public function close():void {
			hideTimer.start();
		}
		private function onHide(event:TimerEvent=null):void {
			hideTimer.stop();
			if (!frame.isFocusOwner()) {
				frame.hide();
//				t_ = new Tween(frame, 0.5, {visible: false, alpha: 0}, true);
//				t_.play();
			}
		}
		private function dedup(list:Array):Array {
			var dict:Dictionary = new Dictionary();
			for (var i:int = 0; i < list.length; ++i) {
				
				dict[JSON.encode(list[i])] = 1;
			}
			list = [];
			for (var l:String in dict) {
				list.push(JSON.decode(l));
			}
			return list;
		}
		public function updateFriend(node:NodeSprite):void {
			var list:Array = [];
			node.visitNodes(function(n:NodeSprite):void {
				var data:Object = n.data;
				var label:JLabel = new JLabel();
				label.setText(data.name);
				label.setIcon(new LoadIcon(data.picture, 40, 40, true));

				
				list.push({'name': data.name, 'picture': data.picture});

				trace(' >' + data.name + ' ' + data.picture);
			});
			list = dedup(list);
			friendList.setListData(list);

		}
		public function updateActivity(node:NodeSprite):void {

			var label:JLabel;
			var products:Array = [
//				['calendar-32.gif', 'New calendar'],
//				['docs_spreadsheets-32.gif', 'New docs'],
				['gmail-32.gif', 'New email'],
//				['sites-32.gif', 'New sites update'],
//				['talk-32.gif', 'New gtalk session'],
				['video_camera-35.gif', 'New youtube video'],
				['buzz.png', 'New Buzz update'],
			];
			var list:Array = [];
			for each (var data:Array in products) {
			
				var picture:String = 'resources/assets/google_apps_icons/' + data[0];

				list.push({'name': data[1], 'picture': picture});
			}

			activityList.setListData(list);

		}
		public function update(node:NodeSprite, stageX:Number, stageY:Number):void {
			var data:Object = node.data;
			frame.setTitle(data.name);
			nameLabel.setText(data.name);
			nameLabel.setIcon(new LoadIcon(data.picture, 50, 50, true));

			frame.setIcon(new LoadIcon(data.picture, 16, 16, true));
			/*
			var xOffset:Number = 0, yOffset:Number = 25;
			frame.x = stageX + xOffset;
			frame.y = stageY + yOffset;
			var b:Rectangle = new Rectangle(0, 0, node.stage.stageWidth, node.stage.stageHeight);
			var r:Rectangle = frame.getBounds(node.stage);
			if (r.width > b.width) {
				frame.x = b.left;
			} else if (r.left < b.left + 5) {
				frame.x = stageX + xOffset;
			} else if (r.right > b.right - 5) {
				frame.x = stageX - 2 - r.width;
			}
			if (r.height > b.height) {
				frame.y = b.top;
				trace(' > 1 ' + frame.y);
			} if (r.top < b.top + 5) {
				frame.y = stageY - yOffset;
				trace(' > 2 ' + frame.y);
			} else if (r.bottom > b.bottom - 5) {
				frame.y = Math.max(stageY - 7 - r.height, 0);
//				trace(' > 3 ' + frame.y + ' ' + r.height);
			}
			*/
			updateActivity(node);
			updateFriend(node);
//			hideTimer.stop();
			frame.show();
		}
		public function hide():void {
			frame.hide();
		}
	}
}